package ciyuan.Player.Skill.Passive;

import ciyuan.Effect.Effects;
import ciyuan.Player.Skill.MySkill;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ShieldItem;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.living.LivingEntityUseItemEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

/**
 * @author 150149
 */
@Mod.EventBusSubscriber
public class SolidShield extends MySkill {

    private static final String ShieldTime = ",cyshieldtime";
    private static final String ShieldDamage = ",cyshielddamage";

    @Override
    public void keyDown(PlayerEntity playerEntity) {

    }

    @Override
    public String getSkillCastName() {
        return "SolidShield";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.16");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.16");
    }

    @SubscribeEvent
    public static void onUse(LivingEntityUseItemEvent event) {
        if (event.getEntityLiving() instanceof PlayerEntity) {
            PlayerEntity playerEntity = (PlayerEntity) event.getEntityLiving();
            SkillUtils.setSkillColdInterval(playerEntity,"SolidShield",300*1000);
            if (!SkillUtils.canCastSkill(playerEntity,"SolidShield")) {
                return;
            }
            if (event.getItem().getItem() instanceof ShieldItem) {
                long total = SkillUtils.getSaveData(playerEntity,ShieldDamage);
                if (!playerEntity.hasEffect(Effects.solid_shield)) {
                    if (total>playerEntity.getMaxHealth()*3) {
                        SkillUtils.setSaveData(playerEntity,ShieldDamage,0);
                        SkillUtils.saveSkillCastTime(playerEntity,"SolidShield");
                        playerEntity.setHealth(playerEntity.getHealth() + total*0.2F);
                    } else {
                        playerEntity.addEffect(new EffectInstance(Effects.solid_shield,60,0));
                    }
                }
            }
        }
    }

    @SubscribeEvent
    public static void onDamage(LivingDamageEvent event) {
        if (event.getEntityLiving() instanceof PlayerEntity) {
            PlayerEntity playerEntity = (PlayerEntity) event.getEntityLiving();
            if (playerEntity.hasEffect(Effects.solid_shield)) {
                SkillUtils.setSaveData(playerEntity,ShieldDamage,(long) (SkillUtils.getSaveData(playerEntity,ShieldDamage)+event.getAmount()));
            }
        }
    }

}
